package com.labyrinth.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;

public class Player extends GameObject
{
    private int direction;// direction of player
    private float height;
    private float width;
    public boolean Moved = false;

    public Player()
    {
    }

    public Player(float x, float y, float height, float width, int inDirection)
    {
        super(x, y);
        this.setScale(width, height);
        setTexture("data/player.png");
        setDrawType(GL10.GL_TRIANGLE_STRIP);
        setMesh();
        direction = inDirection;
    }

    public void move(int direction)
    {
        float speedX = 0;
        float speedY = 0;
        if (direction == 1)
        {
            speedX = 0;
            speedY = 2.5f;
        }
        else if (direction == 2)
        {
            speedX = 2.5f;
            speedY = 0;
        }
        else if (direction == 3)
        {
            speedX = 0;
            speedY = -2.5f;
        }
        else if (direction == 4)
        {
            speedX = -2.5f;
            speedY = 0;
        }
        position.setX(getX() + speedX);
        position.setY(getY() + speedY);
    }

    public void MoveCenter()
    {

    }

    public void setMesh()
    {
        mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
        mesh.setVertices(new float[] { -0.5f, -0.5f, 0, 0, 1, 0.5f, -0.5f, 0, 1, 1, -0.5f, 0.5f, 0, 0, 0, 0.5f, 0.5f, 0, 1, 0 });
        mesh.setIndices(new short[] { 0, 1, 2, 3 });
        vertexCount = 4;

    }

    public void collisionDetection(GameObject[][] brickLevel)
    {
        float brickScaleX = brickLevel[0][0].getScaleX();
        float brickScaleY = brickLevel[0][0].getScaleY();
        int newDirection;
        boolean collision1 = false;
        boolean collision2 = false;

        int pass=0;
        while(pass<=1)
        {
	        if (direction == 1)
	        {
	            if (getY() - (brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getY()) <= 20
	                    && (getY()-(brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getY()) > 15))
	            {
	                if((int) (getY() / brickScaleY) + 1 >= brickLevel.length)
	                {
	                    direction = 3;
	                    move(direction);

	
	                    return;
	                }
	
	                if (brickLevel[(int) (getY() / brickScaleY) + 1][(int) (getX() / brickScaleX)].getClass().equals(MovableBrick.class))
	                {
	                    newDirection = ((MovableBrick) brickLevel[(int) (getY() / brickScaleY) + 1][(int) (getX() / brickScaleX)]).getDirection();
	                    if(collision1)
	                        collision2=true;
	                    collision1=true;
	                    if (direction == newDirection)
	                    {
	                        direction = 3;
	                    }
	                    else
	                    {
	                        direction = newDirection;
	                    }
	                }
	            }
	        }
	
	        if (direction == 2)
	        {
	            if (getX()-(brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getX() ) <= 20
	                    && (getX()-brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getX() ) > 15)
	            {
	                if((int) (getX() / brickScaleX) + 1 >= brickLevel[0].length)
	                {
	                    direction = 4;
	                    move(direction);

	                    return;
	                }
	
	                if (brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX) + 1].getClass().equals(MovableBrick.class))
	                {
	                    newDirection = ((MovableBrick) brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX) + 1]).getDirection();
	                    if(collision1)
	                        collision2=true;
	                    collision1=true;
	                    if (direction == newDirection)
	                    {
	                        direction = 4;
	                    }
	                    else
	                    {
	                        direction = newDirection;
	                    }
	                }
	            }
	        }
	
	        if (direction == 3)
	        {
	            if (( brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getY()-getY()) <= 20
	                    && ( brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getY()-getY()) > 15)
	            {
	                if((int) (getY() / brickScaleY) - 1 < 0)
	                {
	                    direction = 1;
	                    move(direction);
	                    return;
	                }
	
	                if (brickLevel[(int) (getY() / brickScaleY) - 1][(int) (getX() / brickScaleX)].getClass().equals(MovableBrick.class))
	                {
	                    newDirection = ((MovableBrick) brickLevel[(int) (getY() / brickScaleY) - 1][(int) (getX() / brickScaleX)]).getDirection();
	                    if(collision1)
	                        collision2=true;
	                    collision1=true;
	                    if (direction == newDirection)
	                    {
	                        direction = 1;
	                    }
	                    else
	                    {
	                        direction = newDirection;
	                    }
	
	                }
	            }
	        }
	
	        if (direction == 4)
	        {
	            if ((  brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getX()-getX()) <= 20
	                    && (  brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getX()-getX()) > 15)
	            {
	                if((int) (getX() / brickScaleX) - 1 < 0)
	                {
	                    direction = 2;
	                    move(direction);

	                    return;
	                }
	
	                if (brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX) - 1].getClass().equals(MovableBrick.class))
	                {
	                    newDirection = ((MovableBrick) brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX) - 1]).getDirection();
	                    if(collision1)
	                        collision2=true;
	                    collision1=true;
	                    if (direction == newDirection)
	                    {
	                        direction = 2;
	                    }
	                    else
	                    {
	                        direction = newDirection;
	                    }
	                }
	            }
	        }
	        pass++;
        }
        if(!collision2)
            move(direction);
    }

    public boolean gameConditionCheck(GameObject[][] brickLevel,Object className)
    {
        float brickScaleX = brickLevel[0][0].getScaleX();
        float brickScaleY = brickLevel[0][0].getScaleY();

        if(brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getClass().equals(className) &&
                Math.abs(brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getY() - getY()) <= brickScaleY-height &&
                Math.abs(brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getX() - getX()) <= brickScaleX-width)
        {
            return true;
        }

        if (direction == 1)
        {
            if ((brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getY() - getY()) <= 1)
            {
                if((int) (getY() / brickScaleY) + 1 >= brickLevel.length)
                {
                    direction = 3;
                    move(direction);

                    return false;
                }
            }
        }

        if (direction == 2)
        {
            if ((brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getX() - getX()) <= 1)
            {
                if((int) (getX() / brickScaleX) + 1 >= brickLevel[0].length)
                {
                    direction = 4;
                    move(direction);

                    return false;
                }
            }
        }

        if (direction == 3)
        {
            if ((getY() - brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getY()) <= 1)
            {
                if((int) (getY() / brickScaleY) - 1 < 0)
                {
                    direction = 1;
                    move(direction);

                    return false;
                }
            }
        }

        if (direction == 4)
        {
            if ((getX() - brickLevel[(int) (getY() / brickScaleY)][(int) (getX() / brickScaleX)].getX()) <= 1)
            {
                if((int) (getX() / brickScaleX) - 1 < 0)
                {
                    direction = 2;
                    move(direction);

                    return false;
                }
            }
        }

        return false;
    }

    @Override
    public void draw()
    {
        Gdx.gl10.glPushMatrix();
        Gdx.gl10.glTranslatef(position.getX(), position.getY(), 0);
        Gdx.gl10.glScalef(scaleX, scaleY, 0);
        Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
        texture.bind();
        mesh.render(drawType, 0, vertexCount);
        Gdx.gl10.glPopMatrix();
    }

    public void setDirection(int direction)
    {
        this.direction = direction;
    }
}
